﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class SoftwareCatalogController : AdminBaseController
    {
        #region 视图
        [AuthorityActionFilter("m_SoftWareCls", Order = 1)]
        [LogPageActionFilter("软件类别管理", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 构造软件类别树
        /// </summary>
        /// <returns>返回软件树视图</returns>
        public ActionResult SoftwareCatalogTree()
        {
            BLL.SoftwareCatalog softwareCatalogBll = new BLL.SoftwareCatalog();
            TreeModelList treeModelList = new TreeModelList();

            List<Model.SoftwareCatalog> allList = softwareCatalogBll.GetModelListByFEntityNo(GlobalConstant.ENTITYNO);

            if (allList.Count > 0)
            {
                List<Model.SoftwareCatalog> modelList = (from c in allList where c.FPreSoftwareCatalogID == null orderby c.FPreSoftwareCatalogID select c).ToList();

                foreach (Model.SoftwareCatalog model in modelList)
                {
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = model.FSoftwareCatalogID.ToString(),
                        Name = model.FSoftwareCatalogName,
                        ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                    };

                    List<Model.SoftwareCatalog> childModelList = (from c in allList where c.FPreSoftwareCatalogID == model.FSoftwareCatalogID orderby c.FSoftwareCatalogID select c).ToList();
                    if (childModelList.Count > 0)
                    {
                        GetChild(childModelList, treeParentItemModel, allList);
                    }

                    treeModelList.Add(treeParentItemModel);
                }

            }

            return View(treeModelList);
        }

        /// <summary>
        /// 递归获取子节点
        /// </summary>
        /// <param name="modelList">当前父节点下的数据</param>
        /// <param name="treeParentItemModel">父节点</param>
        /// <param name="AllList">全部数据</param>
        private void GetChild(List<Model.SoftwareCatalog> modelList, TreeItemModel treeParentItemModel, List<Model.SoftwareCatalog> allList)
        {
            foreach (Model.SoftwareCatalog model in modelList)
            {

                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = model.FSoftwareCatalogID.ToString(),
                    Name = model.FSoftwareCatalogName,
                    ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                };

                List<Model.SoftwareCatalog> childModelList = (from c in allList where c.FPreSoftwareCatalogID == model.FSoftwareCatalogID orderby c.FSoftwareCatalogID select c).ToList();

                if (childModelList.Count > 0)
                {
                    GetChild(childModelList, treeChildItemModel, allList);
                }

            }

        }
        /// <summary>
        /// 软件类别内容页
        /// </summary>
        /// <param name="catalogID">软件类别编号</param>
        /// <param name="addType">增加类型 增同级 T ，增下级 X,修改为E </param>
        /// <returns>返回软件类别增加或修改视图</returns>
        public ActionResult SoftwareCatalogContent(decimal? catalogID, string addType)
        {
            Model.SoftwareCatalog catalogModel = new Model.SoftwareCatalog();
            if (null != catalogID)
            {
                BLL.SoftwareCatalog softwareCatalogBll = new BLL.SoftwareCatalog();
                catalogModel = softwareCatalogBll.GetModelByIDEntityNo(decimal.Parse(catalogID.ToString()), GlobalConstant.ENTITYNO);

                if (addType == "T")//增加同级
                {
                    catalogModel.FSoftwareCatalogID = 0;
                    catalogModel.FSoftwareCatalogName = "";
                }
                else if (addType == "X")//增加下级
                {
                    catalogModel.FPreSoftwareCatalogID = catalogModel.FSoftwareCatalogID;
                    catalogModel.FSoftwareCatalogID = 0;
                    catalogModel.FSoftwareCatalogName = "";
                }
                else//修改，addType=="E"
                {
                }


                ViewData["preID"] = catalogModel.FPreSoftwareCatalogID;
                ViewData["addType"] = addType;
                return View(catalogModel);
            }
            else
            {
                return View(catalogModel);
            }
            
        }

        public ActionResult Save()
        {
            return View();
        }

        #endregion

        #region AJAX

        /// <summary>
        /// 保存软件类别
        /// </summary>
        /// <param name="addType">增加类型，T为增加同级，X为增加下级，E为修改</param>
        /// <param name="preID">上级软件类别</param>
        /// <param name="catalogID">软件类别</param>
        /// <param name="catalogName">软件名称</param>
        /// <returns>返回结果</returns>
        public string CatalogSave( string addType, decimal? preID,decimal catalogID,string catalogName)
        {
            string returnValue = "";
            BLL.SoftwareCatalog catalogBll = new BLL.SoftwareCatalog();
            try
            {
                Model.SoftwareCatalog model = new Model.SoftwareCatalog();

                model.FEntityNo = GlobalConstant.ENTITYNO;
                model.FPreSoftwareCatalogID = preID;
                model.FSoftwareCatalogName = catalogName;
                if (addType == "")
                {
                    addType = "X";
                }
                if (addType == "T")//增加同级
                {
                    catalogBll.Add(model);
                }
                else if (addType == "X")//增加下级
                {
                    catalogBll.Add(model);
                }
                else if (addType == "E")//修改
                {
                    model.FSoftwareCatalogID = catalogID;
                    catalogBll.Update(model);
                }
                returnValue = "ok";
            }
            catch
            {
                returnValue = "error";
            }
            return returnValue;
        }

        public string DelCatalog(decimal catalogID)
        {
            string returnValue = "";
            BLL.SoftwareCatalog catalogBll = new BLL.SoftwareCatalog();
            BLL.SoftwareEntry entryBll=new BLL.SoftwareEntry();
            try
            {
                string entityNo = GlobalConstant.ENTITYNO;
                //先判断是否有下级
                if (catalogBll.ExistNo(catalogID, entityNo))
                {
                    returnValue = "EXISTCHILD";
                }
                else
                {
                    if (entryBll.ExistByCatalogID(catalogID))//判断是否存在当前类别的软件
                    {
                        returnValue = "EXISTSOFT";
                    }
                    else
                    {
                        catalogBll.Delete(catalogID);
                        returnValue = "OK";
                    }
                }
            }
            catch
            {
                returnValue = "ERROR";
            }
            return returnValue;
        }

        #endregion
    }
}
